//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    ///<summary>
    /// Represents a set of properties for a sheet
    ///</summary>
    public class SheetProperties : IOfficeXmlSerializable, INotifyPropertyChanged
    {
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("sheetPr"))
            {
                _syncHorizontal = c.GetOptionalBoolean("syncHorizontal", false);
                _syncVertical = c.GetOptionalBoolean("syncVertical", false);
                _syncRef = c.GetOptionalString("syncRef");
                TransitionEvaluation = c.GetOptionalBoolean("transitionEvaluation", false);
                _transitionEntry = c.GetOptionalBoolean("transitionEntry", false);
                Published = c.GetOptionalBoolean("published", true);
                _codeName = c.GetOptionalString("codeName");
                _filterMode = c.GetOptionalBoolean("filterMode", false);
                _enableFormatConditionsCalculation = c.GetOptionalBoolean("enableFormatConditionsCalculation", true);
                c.PassAttributes();
                if (c.ReaderLocalName == "tabColor")
                    TabColor = SpreadsheetColor.ReadColor("tabColor", c); //\\null OK?
                OutlineProperties = c.ReadOptionalElement<OutlineProperties>("outlinePr");
                PageSetupProperties = c.ReadOptionalElement<PageSetupProperties>("pageSetUpPr");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("sheetPr"))
            {
                c.SetOptionalBoolean("syncHorizontal", _syncHorizontal, false);
                c.SetOptionalBoolean("syncVertical", _syncVertical, false);
                c.SetOptionalString("syncRef", _syncRef);
                c.SetOptionalBoolean("transitionEvaluation", TransitionEvaluation, false);
                c.SetOptionalBoolean("transitionEntry", _transitionEntry, false);
                c.SetOptionalBoolean("published", Published, true);
                c.SetOptionalString("codeName",_codeName) ;
                c.SetOptionalBoolean("filterMode", _filterMode, false);
                c.SetOptionalBoolean("enableFormatConditionsCalculation", _enableFormatConditionsCalculation, true);
                if (TabColor != null)
                    TabColor.WriteXmlAs("tabColor", c);
//                if (c.Reader.LocalName == "outlinePr")
  //                  throw new NotImplementedException();
                c.WriteOptionalElement(OutlineProperties);
                c.WriteOptionalElement (_pageSetUpPr);
            }
        }

        #endregion

        private OutlineProperties _outlinePr;

        public OutlineProperties OutlineProperties
        {
            get { return _outlinePr; }
            set { _outlinePr = value; }
        }
	

        private PageSetupProperties _pageSetUpPr;

        public PageSetupProperties PageSetupProperties
        {
            get { return _pageSetUpPr; }
            set { _pageSetUpPr = value; }
        }
	

        private bool _enableFormatConditionsCalculation = true;

        public bool EnableFormatConditionsCalculation
        {
            get { return _enableFormatConditionsCalculation; }
            set { _enableFormatConditionsCalculation = value; }
        }


        private bool _filterMode;

        public bool FilterMode
        {
            get { return _filterMode; }
            set { _filterMode = value; }
        }


        private string _codeName;

        public string CodeName
        {
            get { return _codeName; }
            set { _codeName = value; }
        }


        private bool _published = true;

        public bool Published
        {
            get { return _published; }
            set { _published = value; }
        }


        private bool _transitionEntry;

        public bool TransitionEntry
        {
            get { return _transitionEntry; }
            set { _transitionEntry = value; }
        }


        private bool _transitionEvaluation;

        public bool TransitionEvaluation
        {
            get { return _transitionEvaluation; }
            set { _transitionEvaluation = value; }
        }


        private string _syncRef; //\\

        public string SyncRef
        {
            get { return _syncRef; }
            set { _syncRef = value; }
        }


        private bool _syncVertical;

        public bool SyncVertical
        {
            get { return _syncVertical; }
            set { _syncVertical = value; }
        }

        private bool _syncHorizontal;

        public bool SyncHorizontal
        {
            get { return _syncHorizontal; }
            set { _syncHorizontal = value; }
        }

        private SpreadsheetColor _tabColor;

        public SpreadsheetColor TabColor
        {
            get { return _tabColor; }
            set { _tabColor = value;this.NotifyPropertyChanged("TabColor"); }
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        internal void NotifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
